package com.fingard.dsp.bank.directbank.mybank01;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by Ross on 2017-06-06.
 */
public class MYBank01SinglePay extends MYBank01Base implements ISinglePay {

    private static final String MONEY_FORMAT = "000000000000000";

    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = d_190802(p_req1908);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    //190802对外支付
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            MYBank01Header bankHeader = new MYBank01Header();

            Date tmpDateNow = new Date();
            bankHeader.reqMsgID = Format.DateTime17Format.format(tmpDateNow);
            bankHeader.reqTime = tmpDateNow;

            if (p_req1908.singleDetail.sameBnk.equals("1")) {
                //同行
                //赋值通用参数
                bankHeader.function = "ant.ebank.transfer.single.linetrans";
                bankHeader.signFields = "bizNo,smsCode,amt,currencyCode,payerCardNo,payerName,payeeAccountNo,payeeName,remark";
                bankHeader.signValueField = "sign";
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);

                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("partner", bankFront.operator);
                xmlWriter.writeElementString("bizNo", p_req1908.singleDetail.askNumber);
                xmlWriter.writeElementString("smsCode", p_req1908.singleDetail.smsCode);
                BigDecimal curUnit = new BigDecimal(100);
                BigDecimal amt = new BigDecimal(p_req1908.singleDetail.payAmount);
                amt = amt.multiply(curUnit).setScale(0, BigDecimal.ROUND_HALF_UP);
                DecimalFormat df = new DecimalFormat(MONEY_FORMAT);
                xmlWriter.writeElementString("amt", df.format(amt));
                xmlWriter.writeElementString("currencyCode", "156");
                xmlWriter.writeElementString("payerCardNo", p_req1908.singleDetail.payAct);
                xmlWriter.writeElementString("payerName", p_req1908.singleDetail.payName);
                xmlWriter.writeElementString("payeeAccountNo", p_req1908.singleDetail.recAct);
                xmlWriter.writeElementString("payeeName", p_req1908.singleDetail.recName);
                xmlWriter.writeElementString("remark", p_req1908.singleDetail.purpose);
                xmlWriter.writeElementString("sign", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();

            } else if (p_req1908.singleDetail.sameBnk.equals("0")) {
                //跨行
                //赋值通用参数
                bankHeader.function = "ant.ebank.transfer.single.crosslinetrans";
                bankHeader.signFields = "bizNo,smsCode,amt,currencyCode,payerCardNo,payerName,payeeAccountNo,payeeName,remark,payeeBankOrgCode,payeeBankBranchOrgCode";
                bankHeader.signValueField = "sign";
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);

                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("partner", bankFront.operator);
                xmlWriter.writeElementString("bizNo", p_req1908.singleDetail.askNumber);
                xmlWriter.writeElementString("smsCode", p_req1908.singleDetail.smsCode);
                BigDecimal curUnit = new BigDecimal(100);
                BigDecimal amt = new BigDecimal(p_req1908.singleDetail.payAmount);
                amt = amt.multiply(curUnit).setScale(0, BigDecimal.ROUND_HALF_UP);
                DecimalFormat df = new DecimalFormat(MONEY_FORMAT);
                xmlWriter.writeElementString("amt", df.format(amt));
                xmlWriter.writeElementString("currencyCode", "156");
                xmlWriter.writeElementString("payerCardNo", p_req1908.singleDetail.payAct);
                xmlWriter.writeElementString("payerName", p_req1908.singleDetail.payName);
                xmlWriter.writeElementString("payeeAccountNo", p_req1908.singleDetail.recAct);
                xmlWriter.writeElementString("payeeName", p_req1908.singleDetail.recName);
                xmlWriter.writeElementString("remark", p_req1908.singleDetail.purpose);
                xmlWriter.writeElementString("payeeBankOrgCode", "", true);
                xmlWriter.writeElementString("payeeBankBranchOrgCode", p_req1908.singleDetail.cnapsCode);
                xmlWriter.writeElementString("sign", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = "同行跨行标识无法识别，请核对";
                return retDetail;
            }

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultMsg");
                    retDetail.reqSeqID = tmpBkReader.getFirstText("/document/response/body/bizNo");
                    retDetail.transState = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultStatus") + "+" + tmpBkReader.getFirstText("/document/response/body/resultInfo/resultCode");
                    retDetail.payInfoCode = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultStatus") + "+" + tmpBkReader.getFirstText("/document/response/body/resultInfo/resultCode");
                    retDetail.outTransID = tmpBkReader.getFirstText("/document/response/body/bussSeqNo");
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }

            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        return retDetail;
    }

    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail();
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //赋值通用参数
            MYBank01Header bankHeader = new MYBank01Header();
            bankHeader.function = "ant.ebank.transfer.singlepay.query";
            bankHeader.signFields = "bizNo,cardNo";
            bankHeader.signValueField = "sign";

            Date tmpDateNow = new Date();
            bankHeader.reqMsgID = Format.DateTime17Format.format(tmpDateNow);
            bankHeader.reqTime = tmpDateNow;

            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("body");
            xmlWriter.writeElementString("partner", bankFront.operator);
            xmlWriter.writeElementString("bizNo", p_req1944.askNumber);
            xmlWriter.writeElementString("cardNo", p_req1944.payAct);
            xmlWriter.writeElementString("sign", "");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultStatus");
                    String tmpRetInfo = tmpBkReader.getFirstText("/document/response/body/resultInfo/resultMsg");
                    if (tmpRetCode != null && tmpRetCode.equals("S")) {
                        tmpRetAtsDetail.respCode = "0";
                        tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/document/response/body/errorDesc");
                        tmpRetAtsDetail.reqSeqID = tmpBkReader.getFirstText("/document/response/body/bizNo");
                        tmpRetAtsDetail.transState = tmpBkReader.getFirstText("/document/response/body/status");
                        tmpRetAtsDetail.payInfoCode = tmpBkReader.getFirstText("/document/response/body/status");

                        BigDecimal curUnit = new BigDecimal(100);
                        BigDecimal transAmt = new BigDecimal(tmpBkReader.getFirstText("/document/response/body/amount"));
                        tmpRetAtsDetail.transAmt = transAmt.divide(curUnit).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                    } else {
                        tmpRetAtsDetail.respCode = "-1";
                        tmpRetAtsDetail.respInfo = tmpRetInfo;
                    }
                } else {
                    tmpRetAtsDetail.respCode = "-1";
                    tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
                }

            } else {
                tmpRetAtsDetail.respCode = "-1";
                tmpRetAtsDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            tmpRetAtsDetail.respCode = "-1";
            tmpRetAtsDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(tmpRetAtsDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
